﻿using System;
using System.Linq;
using System.Windows.Forms;
using UtfprLibrary.Windows.Forms.ComponentModel;
using UtfprLibrary.Windows.Forms.Exceptions;

namespace UtfprLibrary.Windows.Forms.Extensions {
    public static class UtfprValidatorExtensions {
        public static UtfprValidatableControl GetValidatableControl(this UBNValidatableControlCollection collection, Control control) {
            try {
                return collection.OfType<UtfprValidatableControl>()
                    .Single(c => c.Control == control);
            } catch (Exception ex) {
                throw new UtfprWFCoreExtensionsException("", ex);
            }
        }

        public static UtfprValidatableControl GetValidatableControl(this UBNRequiredFieldsValidatorBase validator, Control control) {
            try {
                return validator.ValidatableControlCollection
                    .OfType<UtfprValidatableControl>()
                    .Single(c => c.Control == control);
            } catch (Exception ex) {
                throw new UtfprWFCoreExtensionsException("", ex);
            }
        }
    }
}
